<template>
    <n-form label-placement="left" label-width="auto">
        <n-form-item-row label="用户号">
            <n-input v-model:value="licenseForm.userId"  placeholder="用户号(QQ号或者手机号等)" clearable>

            </n-input>
        </n-form-item-row>
      <n-form-item-row label="ip码">
        <n-input type="textarea" v-model:value="licenseForm.ipAddressStr"  readonly>
        </n-input>
      </n-form-item-row>
      <n-form-item-row label="mac码">
        <n-input type="textarea" v-model:value="licenseForm.macAddressStr" readonly>
        </n-input>
      </n-form-item-row>
    </n-form>
    <n-button block :loading="loading" @click="applyLicense">申 请</n-button>
</template>

<script lang="ts">
import {defineComponent, onMounted, reactive, ref,} from "vue";
import {BagOutline, Moon, Person, QrCode, SunnySharp} from '@vicons/ionicons5';
import {userController} from "@/api";

export default defineComponent({
    name: "licenseForm",
    props: {
        isSuccess: {
            type: [Boolean, String],
            default: false
        }
    },
    setup(props, {emit}) {
        // 加载中
        const loading = ref<boolean>(false)
        // 验证码
        const captchaCode = ref<string | undefined>(undefined)
        const licenseForm = reactive({
            userId: "",
            ipAddress: [],
            macAddress: [],
            cpuSerial: "",
            mainBoardSerial: "",
            ipAddressStr: "",
            macAddressStr: ""
        })
        // 登录

        const getPCInfo = () =>{
          userController.getPCInfo().then(res => {
            if (res.code===200){
              licenseForm.ipAddress = res.data.ipAddress
              licenseForm.macAddress = res.data.macAddress
              licenseForm.cpuSerial = res.data.cpuSerial
              licenseForm.mainBoardSerial = res.data.mainBoardSerial
              if (licenseForm.ipAddress.length>0){
                licenseForm.ipAddressStr = licenseForm.ipAddress.join("\n")
                licenseForm.macAddressStr = licenseForm.macAddress.join("\n")
              }
            }
          })
        }

        const applyLicense = () =>{
          if (licenseForm.userId===""){
            window.$message.error("用户号不能为空")
            return
          }
          loading.value = true
          userController.applyLicense(licenseForm.userId).then(res => {
            if (res.code==200){
               window.$message.success("生成成功,请重启应用,加载证书！！")
            }
          }).finally(() => {
            loading.value = false
          })
        }

        onMounted(() => {
          getPCInfo()
        })

        return {
            loading,
            licenseForm,
            captchaCode,
            getPCInfo,
            applyLicense,
            BagOutline, Moon, Person, QrCode, SunnySharp
        }
    }
})
</script>

<style scoped>

</style>
